<?php namespace Home\Controller; 
use Think\Controller; 
class IndexController extends CommonController
{    //首页
	public function index(){
		//置顶加精
		$sql="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state<>'0' order by t1.state desc";
		$res=M()->query($sql);
		$this->assign('info',$res);

		//首页分页
		$count =  M('question')->where("state='0'")->count();
		$Page = new \Think\Page($count,3);
		$show = $Page->show();
		$this->assign('page',$show);

		//普通贴

		$sqls="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state='0' order by t1.updata desc limit ".$Page->firstRow.",".$Page->listRows;
		// var_dump($sqls);exit;
		$ress=M()->query($sqls);

		// echo "<pre/>";
		// var_dump($ress);exit;
		$this->assign('infos',$ress);

		$sql="select id,nickname,face,answer_num from user where ustate='0' order by answer_num desc limit 12";
		$res=M('user')->query($sql);
		$this->assign('users',$res);

		$sql="select title,id,view_num from question order by view_num desc limit 10";
		$res=M('user')->query($sql);
		$this->assign('hotview',$res);

		$sql="select title,id,answer_num from question order by view_num desc limit 10";
		$res=M('user')->query($sql);
		$this->assign('hotanswer',$res);

		// 获取友情链接并存入缓存
    	if (S('linkdata')) {
    		$data = S('linkdata');

    	}else{
    		$data = $this->getlink();
    		S('linkdata',$data,600);

    	}    	
		$this->assign('link',$data);
		$this->display();


	}
	//获取友情链接
	public function getlink()
	{
		return $data=M('link')->select();
		
	}
	//未采纳
	public function unsolved()
	{	
		//分页
		$count =  M('question')->where("status='0'")->count();
		$Page = new \Think\Page($count,10);
		$show = $Page->show();
		$this->assign('page',$show);



		$sqls="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status='0' order by t1.updata,t1.state desc limit ".$Page->firstRow.",".$Page->listRows;
		$ress=M()->query($sqls);
		$this->assign('infos',$ress);
		$this->display();
	}

	//己采纳
	public function solved()
	{	


		//分页
		$count =  M('question')->where("status='1'")->count();
		$Page = new \Think\Page($count,10);
		$show = $Page->show();
		$this->assign('page',$show);



		$sqls="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status='1' order by t1.updata,t1.state  limit ".$Page->firstRow.",".$Page->listRows;
		$ress=M()->query($sqls);
		$this->assign('infos',$ress);
		$this->display();
	}

	
	//精贴
	public function wonderful()
	{	
		//分页
		$count =  M('question')->where("state='2'")->count();
		$Page = new \Think\Page($count,10);
		$show = $Page->show();
		$this->assign('page',$show);



		$sqls="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.state='2' order by t1.updata limit ".$Page->firstRow.",".$Page->listRows;
		$ress=M()->query($sqls);
		$this->assign('infos',$ress);
		$this->display();
	}
	 // 搜索
	public function search()
	{
		// var_dump($_GET);
		$q=$_GET['q'];

		// var_dump($q);
		Vendor('pscws4.pscws4','','.class.php');

		$pscws = new \PSCWS4('utf8');

		$pscws->set_dict('./public/dict.utf8.xdb');

		$pscws->send_text($q);

		$tops = $pscws->get_tops(3, 'n,v');

		$sql="select t1.title, t1.view_num, t1.answer_num, t1.create_time, t1.state, t1.id, t1.uid, t2.nickname, t2.face, t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where";
		
		$sql1= "select count(*) as count from question where";

		
		if ($tops) {//判断是否取到词

			foreach ($tops as  $row) {
				$sql .=" t1.title like '%".$row['word']."%' or ";
				$sql1 .=" title like '%".$row['word']."%' or ";

			}

			$sql = rtrim($sql,'or ');
			$sql1= rtrim($sql1,'or ');
		}else{

			$sql .= " t1.title like '%".$q."%'";
			$sql1 .=" title like '%".$q."%'";

		}

		$count=M('question')->query($sql1);

		$count=current($count)['count'];

		$Page = new \Think\Page($count,10);
		$show = $Page->show();
		$this->assign('page',$show);

		$sql=$sql." limit ".$Page->firstRow.",".$Page->listRows;


		$res = M()->query($sql);

		if ($tops) {
			foreach ($res as $k => $v) {
				foreach ($tops as $kk => $vv) {
					$res[$k]['title'] = str_replace($vv['word'],"<span style='color: #c00; background: none; padding: 0; margin: 0; font-size: 16px;'>".$vv['word']."</span>",$v['title']);
				}
			}
		}else{
			foreach ($res as $k => $v) {
				$res[$k]['title'] = str_replace($q,"<span style='color: #c00; background: none; padding: 0; margin: 0; font-size: 16px;'>".$q."</span>",$v['title']);
			}

		}
		
		$this->assign('count',$count);
		$this->assign('info',$res);

		$this->display();

	}


}
